* ======================================
* ---------- Import Workplace ----------
* ======================================


	use "$dta_raw\dta5\AMELD_2015_M9.dta",clear	// Work data in Sept 2015
	rename w19_0078_lopenr_pers idPers
	rename w19_0078_lopenr_bednr idWork2015		// Bedrift = "establishment"
	rename virk_nace1_sn07 nace07 				// NACE sector identifier
	rename arb_avtalte_aarsverk posfrac_raw		// position fraction (FTE)
	drop if idWork == ""
	
	* Calculate size
	gen one = 1
	bysort idWork2015: egen WorkSize = total(one)
	
	* Calculate implied position fraction from earnings if missing
	recode lonn_* (mis = 0) 							// Impute 0 if missing
	gen earnings = lonn_kontant + lonn_natural + lonn_godt
	replace earnings = 0 if earnings < 0				// Set <0 to 0
	bysort idPers: egen earnings_tot = sum(earnings) 
	gen posfrac = posfrac_raw
	replace posfrac = 0 if posfrac_raw == .
	replace posfrac = earnings/earnings_tot if posfrac == 0 & earnings_tot > 0

	* Keep only main (=highest fraction, if multiple) job
	bysort idPers (posfrac idWork): keep if _n==_N
	
	keep id* nace WorkSize
	sort id* nace WorkSize
	
	save "$dta\ImportWork15.dta",replace